package com.smartadmin.module.business.people;

import com.github.pagehelper.PageInfo;
import com.smartadmin.common.controller.BaseController;
import com.smartadmin.common.domain.ResponseParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = {"用户信息"})
@RestController
public class PeopleController extends BaseController {

    @Resource
    private PeopleService peopleService;

    @ApiOperation(value = "分页查询用户列表",notes = "")
    @PostMapping("/people/list")
    public ResponseParam queryByPage(@RequestBody People people) {
        PageInfo<People> pageInfo = peopleService.selectPeopleList(people);// NumberUtils.toInt()
        return buildRespResult(pageInfo);
    }

    @ApiOperation(value = "查询所有部门",notes = "")
    @PostMapping("/people/queryDepartmentById")
    public ResponseParam queryDepartmentById() {
        List<DeptCategory> departmentList = peopleService.queryDepartmentById();
        return buildRespResult(departmentList);
    }

    @ApiOperation(value = "新增用户",notes = "")
    @PostMapping("/people/insert")
    public ResponseParam insertPeople(@RequestBody People people) {
        try {
            peopleService.insertPeople(people);
            return buildRespResult("200", "新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return buildRespResult("500", "新增失败！");
        }

    }

    @ApiOperation(value = "查询用户详情",notes = "")
    @PostMapping("/people/details")
    public ResponseParam queryPeopleDetails(@RequestBody People people) {
        People people1 = peopleService.queryPeopleDetails(people);
        return buildRespResult(people1);
    }

    @ApiOperation(value = "查询用户详情",notes = "")
    @PostMapping("/people/update")
    public ResponseParam updatePeople(@RequestBody People people) {
        People people1 = peopleService.updatePeople(people);
        return buildRespResult(people1);
    }

    @ApiOperation(value = "逻辑删除用户",notes = "")
    @PostMapping("/people/delete")
    public ResponseParam deletePeople(@RequestBody People people) {
        try {
            peopleService.deletePeople(people);
            return buildRespResult("200", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return buildRespResult("500", "删除失败");
        }
    }

}
